#ifndef SERVER_CORE_H
#define SERVER_CORE_H
#include <QObject>
#include <QHash>
#include <QJsonDocument>
#include "server_network.h" // 依赖网络层
#include"user_database.h"
#include"message_util.h"
#include"friend_database.h"
#include"group_database.h"
class ServerCore : public QObject {
    Q_OBJECT
public:
    explicit ServerCore(ServerNetwork *network, QObject *parent = nullptr);


private slots:
    // 处理网络层传递的原始数据
    void handleRawData(QTcpSocket *socket, const QByteArray &data);
    void handleClientDisconnected(QTcpSocket *socket);

private:
    ServerNetwork *network; // 依赖网络层对象
    QHash<QString, QTcpSocket*> userToSocket; // 用户名到套接字的映射（业务层维护）
    DatabaseConnection dbConnection;
    UserDatabase *userDb;
    MessageUtil message;
    FriendDatabase *friendDb;
    GroupDatabase *groupDb;
    QList<User> allUser;

    void sendPrivateChatResponse(QTcpSocket *socket, const QJsonObject &chat,const QString &msg);

    // 消息解析（业务逻辑）
    //Message parseMessage(const QByteArray &data);
    // 用户验证（业务逻辑）
    bool verifyUser(const QString &username, const QString &password);
    bool RegisterUser(const QString &username,const QString &password);
    void sendTransferAck(QTcpSocket *socket, const QString &fileName, bool success,
                         MessageType responseType, const QString &errorMsg = "");
    bool isValidImageFormat(const QString &fileName);
};


#endif // SERVER_CORE_H
